const { Keypair } = require("@solana/web3.js");

const config = require('./config');
const bip39 = require('bip39');
const ed25519 = require('ed25519-hd-key');
const { sha256 } =require("@cosmjs/crypto");
const { toHex } =require("@cosmjs/encoding");
const getSeedFromMne = async () => {
    return await bip39.mnemonicToSeed(config.mnemonic);
}

const getKeyPair = async (accountPath)=>{
    const seed = await getSeedFromMne();
    const userSeed = ed25519.derivePath(accountPath,seed.toString('hex')).key;
    const user_keypair = Keypair.fromSeed(userSeed);
    const user_addr = user_keypair.publicKey.toBase58();
    const user_privKey = user_keypair.secretKey;

    console.log('user_addr: ', user_addr);
    //console.log('user_privKey: ', user_privKey);
    return user_keypair;

}

const getKeyPairUser1 = async () => {
    return await getKeyPair(config.account1.path);
}
const getKeyPairUser2 = async () => {
    return await getKeyPair(config.account2.path);
}
const getKeyPairFromSeed =  () => {
    let userSeed = config.privateKey
    console.log('userSedd: ', userSeed);

    return  Keypair.fromSeed(sha256(userSeed))
}


module.exports = {
    getKeyPairUser1, getKeyPairUser2,getKeyPairFromSeed
}